package com.gsj.maintain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsj.common.pojo.R;
import com.gsj.maintain.mapper.CarMapper;
import com.gsj.maintain.mapper.MtRecordMapper;
import com.gsj.maintain.pojo.entity.TabMtRecord;
import com.gsj.maintain.pojo.entity.TabMtShop;
import com.gsj.maintain.pojo.entity.TbCar;
import com.gsj.maintain.pojo.vo.MtRecordVo;
import com.gsj.maintain.service.MtRecordService;
import com.gsj.maintain.utils.SystemTimeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


/**
 * @program: clms-nine
 * @description: 维修记录业务层
 * @author: 郭书江
 * @create: 2021-07-23 14:48
 **/


@Service
public class MtRecordServiceImpl implements MtRecordService {
    @Autowired
    private MtRecordMapper recordMapper;

    @Autowired
    private CarMapper carMapper;

    //查询维修记录表
    @Override
    public R findMtRecordByPage(Integer currentPage, Integer pageSize, MtRecordVo mtRecordVo) {
        Page<TabMtRecord> page = new Page<>(currentPage,pageSize);
        QueryWrapper<TabMtRecord> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(mtRecordVo.getShopName())){
            wrapper.like("ms.name",mtRecordVo.getShopName());
        }
        if (StringUtils.isNotEmpty(mtRecordVo.getCarName())){
            wrapper.like("c.car_name",mtRecordVo.getCarName());
        }
        if (StringUtils.isNotEmpty(mtRecordVo.getStartDate())){
            wrapper.ge("mr.add_time",mtRecordVo.getStartDate());
        }
        wrapper.orderByDesc("record_id");
        IPage<TabMtRecord> page1= recordMapper.selectRecordAll(page,wrapper);

        return R.ok().put("records",page1.getRecords()).put("total",page1.getTotal());
    }

    //修改的回显
    @Override
    public R selById(Integer id) {
        TabMtRecord tabMtRecord = recordMapper.selectById(id);

        return R.ok().put("MtRecord",tabMtRecord);
    }

    //删除
    @Override
    public R del(Integer id) {
        int i = recordMapper.deleteById(id);

        if (i>0){
            return R.ok().put("message","成功");
        }
        return R.error().put("message","失败");
    }

    //添加
    @Override
    public R insert(TabMtRecord mtRecord,String username) {
        String time = SystemTimeUtil.getTime();
        mtRecord.setOperationTime(time);
        String timeLong = SystemTimeUtil.getTimeLong();
        mtRecord.setRecordId(timeLong);
        //前台获取的操作人
        mtRecord.setOperator(username);
        int i = recordMapper.insert(mtRecord);
        if (i>0){
            return R.ok().put("message","成功");
        }
        return R.error().put("message","失败");
    }

    //修改
    @Override
    public R up(TabMtRecord mtRecord) {
        UpdateWrapper<TabMtRecord> wrapper = new UpdateWrapper<>();
        String time = SystemTimeUtil.getTime();
        mtRecord.setOperationTime(time);
//        wrapper.set("car_id", mtRecord.getCarId());
//        wrapper.set("maintain_shop_id", mtRecord.getMaintainShopId());
        wrapper.set("amount", mtRecord.getAmount());
//        wrapper.set("add_time", mtRecord.getAddTime());
        wrapper.set("operation_time", mtRecord.getOperationTime());
//        wrapper.set("remark", mtRecord.getRemark());
        wrapper.eq("id",mtRecord.getId());
        int i = recordMapper.update(null, wrapper);
        if (i>0){
            return R.ok().put("message","成功");
        }
        return R.error().put("message","失败");
    }
    //查询汽车
    @Override
    public R selectCarListAll() {
        List<TbCar> tbCars = carMapper.selectList(null);
        return R.ok().put("tbCars",tbCars);
    }

    //
    @Override
    public R getCarById(Integer id) {
        TabMtRecord carById = recordMapper.getCarById(id);
        return R.ok().put("car",carById);
    }

    //校验汽车id是否重复
    @Override
    public R selMtRecordCarId(TabMtRecord mtRecord) {
        List<TabMtRecord> tabMtRecord = recordMapper.selectList(null);
        int result=0;
        for (TabMtRecord record : tabMtRecord) {
            if (mtRecord.getId()!=null){
                if (!mtRecord.getId().equals(record.getId())){
                    if (mtRecord.getCarId().equals(record.getCarId())){
                        result=1;
                        break;
                    }
                }
            }else {
                if (mtRecord.getCarId().equals(record.getCarId())){
                    result=1;
                    break;
                }
            }
        }
        return R.ok().put("code",result);
    }

}
